package com.secsbrain.development.web.controller;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.secsbrain.common.ResponseResult;
import com.secsbrain.common.utils.RedisUtil;
import com.secsbrain.deploy.client.iface.DeployService;
import com.secsbrain.development.biz.IBranchService;
import com.secsbrain.development.biz.model.BranchMerge;
import com.secsbrain.frame.lock.Callback;
import com.secsbrain.frame.lock.DistributedLockTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.secsbrain.deploy.client.model.JobNameBuildParam;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@RequestMapping("interface")
@ApiIgnore
public class InterfaceController {
	
    private static final Logger logger = LoggerFactory.getLogger(InterfaceController.class);

    @Autowired
    private DistributedLockTemplate distributedLockTemplate;
    
    @Autowired
    private IBranchService branchService;
    
    @Autowired
    private DeployService deployRemoteService;
    
    /**
     * 功能描述: <br>
     * 合并分支接口
     *
     * @param branchMerge
     * 
     * @return
     */
    @RequestMapping("mergeBranch")
    @ResponseBody
    public ResponseResult mergeBranch(BranchMerge branchMerge){
        ResponseResult baseResult=branchService.mergeBranch(branchMerge);
        logger.debug("合并分支结果：{}",baseResult);
        return baseResult;
    }
    
    @RequestMapping("test")
    public void test() {
//    	cmdbService.queryAppDetailById(2L);

    }
    
    @RequestMapping("testRedis")
    public void testRedis() {
    	try {
    		String val= RedisUtil.get("zhaoyi_test");
    		System.out.println(val);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    }
    
    @RequestMapping("testDeploy")
    public void testDeploy() {
    	JobNameBuildParam jobNameBuildParam=new JobNameBuildParam();
    	jobNameBuildParam.setJobName("zhaoyi_test");
    	Map<String, String> params=new HashMap<String, String>();
    	params.put("name", "zhaoyi");
    	jobNameBuildParam.setJsonParams(JSON.toJSONString(params));
		deployRemoteService.buildByJobName(jobNameBuildParam);
    }
    
    @RequestMapping("testLock")
    public void testLock() {
    	 distributedLockTemplate.execute("insert_news_"+1, 500, new Callback() {
             
             @Override
             public Object onGetLock() throws InterruptedException {
                 logger.debug("新增评论：{}",11);
                 Thread.sleep(2000);
                 return null;
             }
             
             @Override
             public Object onTimeout() throws InterruptedException {
                 logger.debug("呵呵，超时了");
                 return null;
             }
             
         });
    	
    }
    
}
